﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace _4DJackPot
{
    public class Company
    {
        public string companyIdString { get; set; }
        public string companyNameString { get; set; }
        public string companyLogoUrl { get; set; }

        public Company(string arg_companyIdString)
        {
            this.companyIdString = arg_companyIdString;
            //select query 작성
            string selectQueryString =
                "SELECT DISTINCT company_name, company_logo_url FROM companies " +
                "WHERE company_id = " + this.companyIdString + "";

            DbConn DbConn = new DbConn();
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);

            foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
            {
                this.companyNameString = row["company_name"].ToString();
                this.companyLogoUrl = row["company_logo_url"].ToString();
            }
        }

        public bool isExistLastGame()
        {
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");

            string selectQueryString =
                @"SELECT TOP 1 COMPANY_GAMES.GAME_ID FROM COMPANY_GAMES, GAMES
                WHERE COMPANY_GAMES.GAME_ID = GAMES.GAME_ID
                AND GAMES.DRAW_DATETIME <= '" + currentDate + "'" +
                @"AND COMPANY_ID = " + this.companyIdString + " ORDER BY COMPANY_GAMES.GAME_ID DESC";

            DbConn DbConn = new DbConn();
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);
            string lastGameIdString = "";
            foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
            {
                lastGameIdString = row["GAME_ID"].ToString();
            }
            
            if(lastGameIdString == "")
            {
                return false;
            }
            else
            {
                return true;
            }
             
        }
        public Game GetLastGameInfo()
        {
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");

            string selectQueryString =
                @"SELECT TOP 1 COMPANY_GAMES.GAME_ID FROM COMPANY_GAMES, GAMES
                WHERE COMPANY_GAMES.GAME_ID = GAMES.GAME_ID
                AND GAMES.DRAW_DATETIME <= '" + currentDate + "'" +
                @"AND COMPANY_ID = " + this.companyIdString + " ORDER BY COMPANY_GAMES.GAME_ID DESC";
               
            DbConn DbConn = new DbConn();
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);
            string lastGameIdString = "";
            foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
            {
                lastGameIdString = row["GAME_ID"].ToString();
            }

            Game lastGame = new Game(lastGameIdString);

            return lastGame;
        }

        public Game GetGameInfo(string drawDateString)
        {
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");

            string selectQueryString =
                @"SELECT TOP 1 COMPANY_GAMES.GAME_ID FROM COMPANY_GAMES, GAMES
                WHERE COMPANY_GAMES.GAME_ID = GAMES.GAME_ID
                AND GAMES.DRAW_DATETIME = '" + drawDateString + "'" +
                @"AND COMPANY_ID = " + this.companyIdString + " ORDER BY COMPANY_GAMES.GAME_ID DESC";

            DbConn DbConn = new DbConn();
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);
            string lastGameIdString = "";
            foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
            {
                lastGameIdString = row["GAME_ID"].ToString();
            }

            Game lastGame = new Game(lastGameIdString);

            return lastGame;
        }


    }
}
